//
//  MineInfoViewController.m
//  JinYong_iOS_Store
//
//  Created by 王厚一 on 2017/10/22.
//  Copyright © 2017年 why. All rights reserved.
//

#import "MineInfoViewController.h"
#import "AboutUsViewController.h"
#import "UserSafetyViewController.h"
#import "SZKCleanCache.h"
#import "LoginViewController.h"
#import "FeedbackViewController.h"

@interface MineInfoViewController ()

@property (nonatomic, strong) UIImageView * photo;
@property (nonatomic, strong) UILabel * name;


@end

@implementation MineInfoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self addTopView];
    [self createUI];
}

#pragma mark - --------------添加头视图----------------
- (void)addTopView{
    UIView * topView = ({
        UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(0), ActualW(375), ActualH(64))];
        CAGradientLayer * gradientLayer = [CAGradientLayer layer];
        gradientLayer.frame = view.bounds;
        //设置渐变区域的起始和终止位置（范围为0-1）
        gradientLayer.startPoint = CGPointMake(0, 0);
        gradientLayer.endPoint = CGPointMake(1, 0);
        //设置颜色数组
        gradientLayer.colors = @[(__bridge id)RGB(22, 125, 255).CGColor, (__bridge id)RGB(0, 178, 255).CGColor];
        //设置颜色分割点（范围：0-1）
        gradientLayer.locations = @[@(0.0f), @(1.0f)];
        [view.layer addSublayer:gradientLayer];
        view;
    });
    [self.view addSubview:topView];
    
    
    UILabel * title = ({
        UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(152.5), ActualH(32), ActualW(72), ActualH(17))];
        label.textColor = RGB(255, 255, 255);
        label.textAlignment = NSTextAlignmentLeft;
        label.text = @"账户设置";
        label.font = FontX(FONTSIZE(17));
        label;
    });
    [topView addSubview:title];
    
    UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(31.5), ActualW(18), ActualH(18))];
    imageView.image = [UIImage imageNamed:@"back"];
    [topView addSubview:imageView];
    
    
    UIButton * backButton = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(0), ActualH(13.5), ActualW(41), ActualH(44))];
    [backButton addTarget:self action:@selector(popAction) forControlEvents:UIControlEventTouchUpInside];
    
    [topView addSubview:backButton];

}


- (void)createUI {
    float maxY = ActualH(65);
    for (int i = 0; i < 5; i++) {
        UIButton * sender = ({
            float btnHeight = (i == 0) ? ActualH(89.5) : ActualH(45);
            UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(0), maxY, ActualW(375), btnHeight)];
            maxY += btnHeight;
            [button addTarget:self action:@selector(enterAction:) forControlEvents:UIControlEventTouchUpInside];
            button.tag = i + 5000;
            button;
        });
        [self.view addSubview:sender];
        UIView * breakLine = ({
            UIView * view = [[UIView alloc] initWithFrame:CGRectMake(ActualW(10), CGRectGetMaxY(sender.frame) - 1, ActualW(355), ActualH(1))];
            view.backgroundColor = RGB(239, 239, 239);
            view;
        });
        [self.view addSubview:breakLine];
        
        UIImageView * enterImage = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(356), (CGRectGetHeight(sender.frame) - ActualH(12)) / 2, ActualW(7.5), ActualH(12))];
        enterImage.image = [UIImage imageNamed:@"Common_next"];
        [sender addSubview:enterImage];
        
        NSArray * titles = @[@"账户安全",@"清除缓存",@"关于我们",@"意见反馈"];
        if (i == 0) {
            self.photo = ({
                UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(16.5), ActualW(57), ActualH(57))];
                [imageView sd_setImageWithURL:[NSURL URLWithString:kRequestLogo] placeholderImage:nil];
                imageView.layer.cornerRadius = ActualW(28.5);
                imageView.layer.masksToBounds = YES;
                imageView;
            });
            [sender addSubview:self.photo];
            
            self.name = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(76.5), ActualH(34), ActualW(180), ActualH(17))];
                label.textColor = RGB(51, 51, 51);
                label.text = kRequestName;
                label.backgroundColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentLeft;
                label.font = FontX(FONTSIZE(16));
                label;
            });
            [sender addSubview:self.name];
            

        }else {
            UILabel * title = ({
                UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(18), ActualW(100), ActualH(15))];
                label.textColor = RGB(76, 78, 98);
                label.text = titles[i - 1];
                label.backgroundColor = RGB(255, 255, 255);
                label.textAlignment = NSTextAlignmentLeft;
                label.font = FontX(FONTSIZE(14));
                label;
            });
            [sender addSubview:title];
        }
    }
    
    UIButton * logout = ({
        UIButton * button = [[UIButton alloc] initWithFrame:CGRectMake(ActualW(10), ActualH(363.5), ActualW(355), ActualH(40))];
        [button addTarget:self action:@selector(logoutAction) forControlEvents:UIControlEventTouchUpInside];
        button.titleLabel.font = FontX(FONTSIZE(18));
        [button setTitle:@"退出登录" forState:UIControlStateNormal];
        [button setTitleColor:RGB(255, 255, 255) forState:UIControlStateNormal];
        button.backgroundColor = RGB(64, 137, 248);
        button.layer.cornerRadius = ActualW(4);
        button;
    });
    
    [self.view addSubview:logout];
    
}


- (void)enterAction:(UIButton *)sender {
    switch (sender.tag) {
        case 5000:
            [self enterUserInfo];
            break;
        case 5001:
            [self enterUserSafety];
            break;
        case 5002:
            [self enterCrashCache];
            break;
        case 5003:
            [self enterAboutus];
            break;
        case 5004:
            [self enterFeedBack];
            break;
        default:
            break;
    }
}

- (void)enterUserInfo {

}

- (void)enterAddress {

}

- (void)enterUserSafety {
    UserSafetyViewController * vc = [[UserSafetyViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)enterAboutus {
    AboutUsViewController * vc = [[AboutUsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)enterCrashCache {
    NSString * message = [NSString stringWithFormat:@"缓存%.2fM",[SZKCleanCache folderSizeAtPath]];
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"是否清除缓存" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SZKCleanCache cleanCache:^{
            [CustomAlert showAlertWithController:self andTitle:@"缓存清除成功" andMessage:nil andTime:1];
        }];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:confirm];
    [alert addAction:cancel];
    
    [self presentViewController:alert animated:YES completion:nil];
}

- (void)enterFeedBack {
    FeedbackViewController * vc = [[FeedbackViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - -------------------------退出登录----------------------------

- (void)logoutAction {
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否退出当前账号" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction * submit = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self logout];
    }];
    UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:submit];
    [alert addAction:cancel];
    [self presentViewController:alert animated:YES completion:nil];
    

}

- (void)logout {
    [[URLFather sharedInstance] logoutWithSuccess:^(NSDictionary *result) {
        if ([Pub resultSuccess:result]) {
            [NSUDUD setBool:NO forKey:kUserLoginStatus];
            [NSUDUD setValue:@"" forKey:kUserId];
            [NSUDUD setValue:@"" forKey:kKey];
            [NSUDUD setValue:@"" forKey:kLogo];
            [NSUDUD setValue:@"" forKey:kName];
            [NSUDUD setValue:@"0" forKey:kOrderUnfinishedNum];
            [NSUDUD setValue:@"0" forKey:kOrderUnfinishedNum];
            [NSUDUD synchronize];
            //退出清除别名
            [JPUSHService setAlias:@"" completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            } seq:0];
            [self changeController];
            sendNotification(NOTIFICATION_LOGOUTSUCCESS);
            kShowSuccess(@"退出成功");
        }
        else{
            kShowText([result objectForKey:@"msg"]);
        }
        
    } failure:^(NSError *error) {
        kShowError(@"请求失败");
    }];

}

- (void)changeController {
    LoginViewController * vc = [[LoginViewController alloc] init];
    vc.replace = YES;
    [self.navigationController pushViewController:vc animated:YES];
}


- (void)popAction {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
